package com.baba.vote.controller;

import com.baba.vote.common.OperReturnDto;
import com.baba.vote.common.VoteErrorCode;
import com.baba.vote.common.VoteException;
import com.baba.vote.dao.VoteActMapper;
import com.baba.vote.dto.request.VoteActBo;
import com.baba.vote.dto.request.VotePeopleBo;
import com.baba.vote.dto.response.VoteActDto;
import com.baba.vote.pojo.VoteAct;
import com.baba.vote.service.FileService;
import com.baba.vote.service.VoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * @author moxinHuoHuo
 * @create 2020/4/6 下午10:46
 */
@RestController
@RequestMapping("/vote")
@Api(value = "投票活动相关")
@Slf4j
public class VoteController {

  @Autowired
  private FileService fileService;

  @Autowired
  private VoteService voteService;


  @PostMapping("/upload-result")
  @ApiOperation(value = "上传投票结果的图片")
  public OperReturnDto uploadResultPic(MultipartFile file) {

    String path = voteService.uploadVoteResult(file);

    return OperReturnDto.formaterSuccess(path);

  }

//  @GetMapping("/down-result")
//  @ApiOperation(value = "下载投票结果的图片")
//  public void downResultPic(HttpServletResponse response) {
//
//    VoteAct pojo = voteService.get();
//    if (pojo.getResultImg() == null) {
//      throw new VoteException(VoteErrorCode.VOTE_RESULT_NOT_UPLOAD);
//    }
//
//    fileService.downloadFile(pojo.getResultImg(), "投票结果图片", response);
//
//  }


  @PostMapping
  @ApiOperation("开启一场投票")
  public OperReturnDto<VoteActDto> add(@RequestBody VoteActBo voteActBo) {

    VoteActDto result = voteService.add(voteActBo);

    return OperReturnDto.formaterSuccess(result);
  }

  @GetMapping
  @ApiOperation("查询投票情况的数据")
  public OperReturnDto<VoteActDto> detail() {

    VoteActDto detail = voteService.detail();

    return OperReturnDto.formaterSuccess(detail);
  }

  @PutMapping("/stop")
  @ApiOperation("暂停投票接口")
  public OperReturnDto stop() {

    Boolean stop = voteService.stop();

    return OperReturnDto.formaterSuccess(stop);
  }

  @DeleteMapping
  @ApiOperation("删除投票接口")
  public OperReturnDto delete() {

    Boolean delete = voteService.delete();

    return OperReturnDto.formaterSuccess(delete);
  }

  @PostMapping("/{voteItemId}/submission")
  @ApiOperation("提交投票接口")
  public OperReturnDto submission(@PathVariable Integer voteItemId,
                                  @RequestBody VotePeopleBo votePeopleBo) {

    Boolean submission = voteService.submission(null, voteItemId, votePeopleBo);

    return OperReturnDto.formaterSuccess(submission);
  }

}
